package Test;

import java.util.concurrent.locks.ReentrantLock;

class Tickt {
    private volatile int ticktnum = 55;
    ReentrantLock lock = new ReentrantLock(true);

    public void sale() {
        lock.lock();
        try {
            if(ticktnum > 0) {
                System.out.println(Thread.currentThread().getName()+"卖出第： "+(ticktnum--)+"  还剩"+ticktnum);
            }
        } finally {
            lock.unlock();
        }
    }
}
public class TestDemo8 {
    public static void main(String[] args) {
        Tickt tickt = new Tickt();
        new Thread(()->{
            for (int i = 1; i <= 55 ; i++) {
                tickt.sale();
            }
        },"a").start();

        new Thread(()->{
            for (int i = 1; i <= 55 ; i++) {
                tickt.sale();
            }
        },"b").start();

        new Thread(()->{
            for (int i = 1; i <= 55 ; i++) {
                tickt.sale();
            }
        },"c").start();
    }
}
